package vn.hanelsoft.newsduet.common;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

@SuppressWarnings({ "deprecation", "unused" })
public class FacebookUtility {

	// public static final String APPLICATION_ID = "279541048850439";
	private Facebook facebook;
	AsyncFacebookRunner asyncFacebookRunner;

	private SharedPreferences preferences;

	private Activity activity;

	public FacebookUtility(Activity activity, String AppID) {
		this.activity = activity;
		facebook = new Facebook(AppID);
		asyncFacebookRunner = new AsyncFacebookRunner(facebook);
	}

	/**
	 * login facebook
	 */
	public void signInFacebook() {
		preferences = activity.getPreferences(Context.MODE_PRIVATE);
		String access_token = preferences.getString("access_token", null);
		long expires = preferences.getLong("access_expires", 0);

		if (access_token != null) {
			facebook.setAccessToken(access_token);
		}

		if (expires != 0) {
			facebook.setAccessExpires(expires);
		}

		if (!facebook.isSessionValid()) {
			facebook.authorize(activity, new String[] { "email",
					"publish_stream" }, new DialogListener() {

				@Override
				public void onCancel() {
					// Function to handle cancel event
					Log.d("TAG", "onCancel");
				}

				@Override
				public void onComplete(Bundle values) {

					// Log.d("TAG", "onComplete");

					// Function to handle complete event
					// Edit Preferences and update facebook acess_token
					SharedPreferences.Editor editor = preferences.edit();
					editor.putString("access_token", facebook.getAccessToken());
					editor.putLong("access_expires",
							facebook.getAccessExpires());
					editor.commit();

				}

				@Override
				public void onError(DialogError error) {
					// Function to handle error
					Log.d("TAG", "onError");

				}

				@Override
				public void onFacebookError(FacebookError fberror) {
					// Function to handle Facebook errors
					Log.d("TAG", "onFacebookError");
				}

			});
		}
	}

	/**
	 * post to facebook
	 */
	public void postToWall() {
		// post on user's wall.
		facebook.dialog(activity, "feed", new DialogListener() {

			@Override
			public void onFacebookError(FacebookError e) {
			}

			@Override
			public void onError(DialogError e) {
			}

			@Override
			public void onComplete(Bundle values) {
			}

			@Override
			public void onCancel() {
			}
		});

	}
	
	public void postToWall(Bundle mBundle) {
		// post on user's wall.
		facebook.dialog(activity, "feed",mBundle, new DialogListener() {

			@Override
			public void onFacebookError(FacebookError e) {
			}

			@Override
			public void onError(DialogError e) {
			}

			@Override
			public void onComplete(Bundle values) {
			}

			@Override
			public void onCancel() {
			}
		});

	}
	/**
	 * get profile of user
	 */
	public void getProfileInformation() {
		asyncFacebookRunner.request("me", new RequestListener() {

			@Override
			public void onMalformedURLException(MalformedURLException e,
					Object state) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onIOException(IOException e, Object state) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onFileNotFoundException(FileNotFoundException e,
					Object state) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onFacebookError(FacebookError e, Object state) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onComplete(String response, Object state) {
				Log.d("Profile", response);
				String json = response;
				try {
					JSONObject profile = new JSONObject(json);
					// getting name of the user
					final String name = profile.getString("name");
					// getting email of the user
					final String email = profile.getString("email");

					activity.runOnUiThread(new Runnable() {

						@Override
						public void run() {
							Log.d("TAG", "Name: "+name+"\n Email: "+email);
						}

					});

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
		});
	}
}
